﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Reflection;
using RibbonEditor.Controls.Rules;

namespace RibbonEditor.Rules
{
    class RuleHelper
    {
        internal static IEnableRule GetIEnableRuleFromXml(XmlNode node)
        {
            IEnableRule rule = null;

            switch (node.Name)
            {
                case "AuthenticationRule":
                    rule = new AuthenticationRule(node);
                    break;
                case "CrmClientTypeRule":
                    rule = new CrmClientTypeRule(node);
                    break;
                case "CrmOfflineAccessStateRule":
                    rule = new CrmOfflineAccessStateRule(node);
                    break;
                case "CrmOutlookClientTypeRule":
                    rule = new CrmOutlookClientTypeRule(node);
                    break;
                case "CustomRule":
                    rule = new CustomRule(node);
                    break;
                case "EntityRule":
                    rule = new EntityRule(node);
                    break;
                case "FormStateRule":
                    rule = new FormStateRule(node);
                    break;
                case "OrRule":
                    //rule = new OrRule(node, true);
                    rule = new OrRule();
                    break;
                case "OutlookItemTrackingRule":
                    rule = new OutlookItemTrackingRule(node);
                    break;
                case "OutlookVersionRule":
                    rule = new OutlookVersionRule(node);
                    break;
                case "PageRule":
                    rule = new PageRule(node);
                    break;
                case "RecordPrivilegeRule":
                    rule = new RecordPrivilegeRule(node);
                    break;
               case "SelectionCountRule":
                    rule = new SelectionCountRule(node);
                    break;
                case "SkuRule":
                    rule = new SkuRule(node);
                    break;
                case "ValueRule":
                    rule = new ValueRule(node);
                    break;
                default:
                    throw new Exception("Unsupported rule type: " + node.Name);
            }

            return rule;
        }

        internal static IDisplayRule GetIDisplayRuleFromXml(XmlNode node)
        {
            IDisplayRule rule = null;

            switch (node.Name)
            {
                case "AuthenticationRule":
                    rule = new AuthenticationRule(node);
                    break;
                case "CrmClientTypeRule":
                    rule = new CrmClientTypeRule(node);
                    break;
                case "CrmOfflineAccessStateRule":
                    rule = new CrmOfflineAccessStateRule(node);
                    break;
                case "EntityPrivilegeRule":
                    rule = new EntityPrivilegeRule(node);
                    break;
                case "EntityPropertyRule":
                    rule = new EntityPropertyRule(node);
                    break;
                case "EntityRule":
                    rule = new EntityRule(node);
                    break;
                case "FormEntityContextRule":
                    rule = new FormEntityContextRule(node);
                    break;
                case "FormStateRule":
                    rule = new FormStateRule(node);
                    break;
                case "MiscellaneousPrivilegeRule":
                    rule = new MiscellaneousPrivilegeRule(node);
                    break;
                case "OrganizationSettingRule":
                    rule = new OrganizationSettingRule(node);
                    break;
                case "OrRule":
                    rule = new OrRule(node);
                    break;
                case "OutlookRenderTypeRule":
                    rule = new OutlookRenderTypeRule(node);
                    break;
                case "OutlookVersionRule":
                    rule = new OutlookVersionRule(node);
                    break;
                case "PageRule":
                    rule = new PageRule(node);
                    break;
                case "ReferencingAttributeRequiredRule":
                    rule = new ReferencingAttributeRequiredRule(node);
                    break;
                case "RelationshipTypeRule":
                    rule = new RelationshipTypeRule(node);
                    break;
                case "SkuRule":
                    rule = new SkuRule(node);
                    break;
                case "ValueRule":
                    rule = new ValueRule(node);
                    break;
                default:
                    throw new Exception("Unsupported DisplayRule type: " + node.Name);
            }

            return rule;
        }

        internal static ITabDisplayRule GetITabDisplayRuleFromXml(XmlNode node)
        {
            ITabDisplayRule rule = null;

            switch (node.Name)
            {
                case "EntityRule":
                    rule = new EntityRule(node);
                    break;
                case "PageRule":
                    rule = new PageRule(node);
                    break;
            }

            return rule;
        }

        internal static UserControl GetRuleUserControl(string ruleName, object rule)
        {
            UserControl ctrl = null;

            Assembly assembly = Assembly.GetExecutingAssembly();
            AssemblyName assemblyName = assembly.GetName();

            Type t = Type.GetType(assemblyName.Name + ".Controls.Rules." + ruleName + "Control");
            ctrl = (UserControl)Activator.CreateInstance(t, new object[] { rule });

            return ctrl;
        }
    }
}
